<template>
	<view class="content">
		<u-sticky bgColor="#fff">
			<u-tabs :list="list" :is-scroll="false" style="" :current="current" @change="change"></u-tabs>
		</u-sticky>
		<view v-if="current==0">
			<view v-for="(item,index) in jobDetailsArr" :key="index">
				<u-card :title="item.title" padding="30" margin="26rpx" :sub-title="item.startTime">
					<view class="" slot="body" @click="toWork(item)">
						<view class="u-body-item-title u-line-2" style="white-space: break-spaces">{{ item.content }}
						</view>
					</view>
					<view slot="foot">
						<uni-collapse>
							<uni-collapse-item title="作业内容">
								<view class="content">
									<text class="text">{{ item.map ? item.map.content : "" }}</text>
								</view>
							</uni-collapse-item>
						</uni-collapse>
					</view>
				</u-card>
			</view>
		</view>


		<view v-else>
			<view v-for="(item,index) in jobDetailsArr2" :key="index">
				<u-card :title="item.title" padding="30" margin="26rpx" :sub-title="item.startTime">

					<view class="" slot="body">

						<view class="u-body-item-title u-line-2" style="white-space: break-spaces">{{ item.content }}
						</view>
					</view>

					<view slot="foot">
						<uni-collapse>
							<uni-collapse-item title="作业内容">
								<view class="content">
									<text class="text">{{ item.map ? item.map.content : "" }}</text>
								</view>
							</uni-collapse-item>
						</uni-collapse>
						<view v-if="item.map">作业提交时间:{{ item.map ? item.map.startTime : "" }}</view>
						<view v-if="item.map.level==null || item.map.level===undefined">状态:老师还未查看</view>
						<view v-if="!(item.map.level==null || item.map.level===undefined)">
							状态:{{ levelStr(item.map.level) }}
						</view>
					</view>
				</u-card>
			</view>
		</view>


	</view>
</template>

<script>
	import {
		getWorkInfoAndWorkStudentByStudentId
	} from '@/api/jz/student/info.js';
	import {
		getInfoId,
		getInforoleKey
	} from '@/utils/auth';

	export default {
		data() {
			return {
				content: "确认要删除吗？",
				list: [{
					name: '未完成'
				}, {
					name: '已完成',
				}],
				current: 0,
				swiperCurrent: 0,
				isAnnounce: null, //0 保存 ； 1 保存并发布
				jobDetailsArr: [], //作业详情
				jobDetailsArr2: [], //作业详情
				show: false,
				selId: null,
				student: null
			}
		},
		mounted() {

		},
		onLoad(data) {
			if(data.data){
				this.student = JSON.parse(decodeURIComponent(data.data))
				
			}
			
		},
		onShow() {
			this.change(0)
			this.getWorkInfoAndWorkStudentByStudentIdFun();
		},
		methods: {
		
			levelStr(val) {
				let strLevel = '老师还未查看';
				switch (val) {
					case 'A':
						strLevel = '优秀';
						break;
					case 'B':
						strLevel = '良好';
						break;
					case 'C':
						strLevel = '及格';
						break;
					case 'D':
						strLevel = '不及格';
						break;
					case 'E':
						strLevel = '未完成';
						break;
					default:
						strLevel = '老师还未查看';
						break;
				}
				return strLevel;
			},

			toWork(val) {
				
				var result = this.fun_date(7, val.startTime)
				if (Math.abs(result) >= 7) {
					this.$modal.msgError("已超过一周，不能答题")
					return
				}
				if (getInforoleKey() === 'student') {
					uni.navigateTo({
						url: '/pages/studentMyWork/homework?worksObj=' + encodeURIComponent(JSON.stringify(val))
					})
				}
			},
			fun_date(aa, time) {
			
				var date1 = new Date(time);
			
				var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间
			
				var date2 = new Date(time);
			
				date2.setDate(date1.getDate());
			
				var time2 = date2.getFullYear() + "-" +
			
					(date2.getMonth() + 1) + "-" + date2.getDate();
			
			
			
				var date3 = new Date();
			
				var timenow = date3.getFullYear() + "-" + (date3.getMonth() + 1) + "-" + date3.getDate();
				var result = this.diffDay(timenow,time2); //return 2
				return result;
			
			},
			 diffDay(lastDate,earlyDate){
			    return (Date.parse(lastDate) - Date.parse(earlyDate))/1000/60/60/24;
			},
			
			

			getNowDate() {
				var myDate = new Date;
				var year = myDate.getFullYear(); //获取当前年
				var mon = myDate.getMonth() + 1; //获取当前月
				var date = myDate.getDate(); //获取当前日
				var hours = myDate.getHours(); //获取当前小时
				var minutes = myDate.getMinutes(); //获取当前分钟
				var seconds = myDate.getSeconds(); //获取当前秒
				var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
				return now;
			},
			publishJob(row) {
				const data = {
					id: row.id,
					isAnnounce: 1,
					startTime: this.getNowDate(),
				}
				editWorkJob(data).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: `发布成功`
						})
						this.change(this.current)
					}
				}).catch((err) => {
					console.error(err);
				});
			},

			editJob(row) {
				uni.navigateTo({
					url: '/pages/work/workInfo/publishJob?worksObj=' + encodeURIComponent(JSON.stringify(row))
				})
			},
			confirm() {
				let data = {
					id: this.selId,
					delFlag: 1
				}
				editWorkJob(data).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: `删除成功`
						})
						this.change(this.current)
					}
				}).catch((err) => {
					console.error(err);
				});
			},
			delJob(id) {
				this.selId = id;
				this.show = true;
			},
			getWorkInfoAndWorkStudentByStudentIdFun() {
				let studentId = null;
				if (this.student) {
					studentId = this.student.id
				} else {
					studentId = getInfoId();
				}
				getWorkInfoAndWorkStudentByStudentId(studentId).then((res) => {
					this.jobDetailsArr = [];
					this.jobDetailsArr2 = [];
					if (res.code == 200) {
						console.log(res)
						for (var i = 0; i < res.data.length; i++) {
							if (res.data[i].map && res.data[i].map.isAnnounce === 1) {
								this.jobDetailsArr2.push(res.data[i])
							} else {
								this.jobDetailsArr.push(res.data[i])
							}
						}
						// this.jobDetailsArr = res.data
						// console.log("jobDetailsArr", this.jobDetailsArr)
						// console.log("jobDetailsArr2", this.jobDetailsArr2)
					}
				}).catch((err) => {
					console.error(err);
				});
			},
			change(index) {
				this.current = index;
				// switch (index) {
				//   case 0:
				//     this.isAnnounce = null
				//     break;
				//   case 1:
				//     this.isAnnounce = 1
				//     break;
				//   case 2:
				//     this.isAnnounce = 0
				//     break;
				// }
			},
		}
	}
</script>

<style scoped>
	page {
		height: 100%;
		background-color: #f2f2f2;
	}
</style>
<style scoped lang="scss">
	.mini-btn {
		margin-left: 5px;
	}

	.u-card-wrap {
		background-color: $u-bg-color;
		padding: 1px;
	}

	.u-body-item {
		font-size: 32 rpx;
		color: #333;
		padding: 20 rpx 10 rpx;
	}

	.u-body-item image {
		width: 120 rpx;
		flex: 0 0 120 rpx;
		height: 120 rpx;
		border-radius: 8 rpx;
		margin-left: 12 rpx;
	}

	.u-line-2,
	.u-line-3,
	.u-line-4,
	.u-line-5 {
		display: unset !important;
	}
</style>